package com.scqkzqtz.basesdk.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.scqkzqtz.basesdk.R;

import java.util.List;

/**
 * RadioGroup + Fragment 实现tab切换的管理器
 * created by michael 2018/4/25
 */
public class RadioTabManager {
    private int fragmentContainerId;
    /**
     * @param fragmentManager     在FragmentActivity为getSupportFragmentManager(),在Fragment中为getChildFragmentManager
     * @param fragmentList        装Fragment的list
     * @param fragmentContainerId Fragment的容器ID
     * @param radioGroup          与Fragment关联的RadioGroup
     * @param index               默认选中项
     */
    public RadioTabManager(final FragmentManager fragmentManager, final List<Fragment> fragmentList,
                           int fragmentContainerId, RadioGroup radioGroup, int index) {

        FragmentTransaction ft = fragmentManager.beginTransaction();

        for (Fragment fragment : fragmentList) {
            ft.add(fragmentContainerId , fragment, fragment.getId() + "");
        }

        //显示选中项fragment,隐藏其他项
        for (int i = 0; i < fragmentList.size() ; i++) {
            Fragment fragment = fragmentList.get(i);
            if ( index < 0 || index >= fragmentList.size())
                 index = 0;//当传入的默认选中项不在fragmentList中时，默认选中第0项
            if (i == index ) {
                ft.show(fragment);
                fragment.setUserVisibleHint(true);
            } else {
                ft.hide(fragment);
                fragment.setUserVisibleHint(false);
            }
            //默认选中index
            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(index);
            radioButton.setChecked(true);
        }

        ft.commit();

        //tab切换监听

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                FragmentTransaction ft = fragmentManager.beginTransaction();
                for (int i = 0; i < fragmentList.size(); i++) {
                    Fragment fragment = fragmentList.get(i);
                    if (id == radioGroup.getChildAt(i).getId()) {
                        ft.show(fragment);
                        fragment.setUserVisibleHint(true);
                        if ( onTabChangeListener != null ) {
                            onTabChangeListener.onTabChange(radioGroup, i);
                        }
                    } else {
                        ft.hide(fragment);
                        fragment.setUserVisibleHint(false);
                    }
                }
                ft.commit();
            }
        });
    }

    public interface OnTabChangeListener {
        void onTabChange(RadioGroup radioGroup, int index);
    }

    private OnTabChangeListener onTabChangeListener;

    public void setOnTabChangeListener(OnTabChangeListener onTabChangeListener) {
        this.onTabChangeListener = onTabChangeListener;
    }
}
